/**
 * 该模块有待进一步整理, 其中很多内容是临时性的.
 */

var FG;
if (!FG)
    FG = {};

FG.InitApp = function(opts) {
    var this_ = this;
    
    this.moduleInfo = {
        name: 'initApp',
        zhName: '初始化杂项',
        desc: '初始化杂项'
    };
    
    this.app = opts.app;
    
    this._initPlotter();
    this._initDBFeatureOptsParsers();
    this._initFeatureModelParsers();
    this._initFeatureWidgets();
    this.app.toolPanel.setDefaultTab(FG.getSetting('default_tool_tab', 'project'));
    
    this.connectSignals();
};

FG.InitApp.prototype._initPlotter = function() {
    this.app.plotter.registerPlotDatatype({
        timeseries: 'timeseries',
        monthlystat: 'category',
        diurnalstat: 'category',
        monthlymean: 'category',
        diurnalmean: 'category',
        rose: 'rose',
        wpdrose: 'rose',
        winddist: 'dist',
        summary: 'summary',
        vertical_summary: 'vertical_summary'
    });
    
    this.app.plotter.registerPlotFunc({
        timeseries: FG.plotTimeseries,
        category: FG.plotCategory,
        rose: FG.plotRose,
        dist: FG.plotDist,
        summary: FG.plotSummary,
        vertical_summary: _.partial(FG.plotSummary, _, _, {vertical: true})
    });
};

FG.InitApp.prototype._initDBFeatureOptsParsers = function() {
    this.app.mapPanel.registerDBFeatureOptsParser(FG.defaultPItemDBFeatureOptsParser, 100);
};

FG.InitApp.prototype._initFeatureModelParsers = function() {
    this.app.mapPanel.registerFeatureModelParser(FG.pitemFeatureModelParser, 100);
    this.app.mapPanel.registerFeatureModelParser(FG.geojsonFeatureModelParser, 200);
    this.app.mapPanel.registerFeatureModelParser(FG.featureFeatureModelParser, 300);
    this.app.mapPanel.registerFeatureModelParser(FG.objFeatureModelParser, 99999);
};

FG.InitApp.prototype._initFeatureWidgets = function() {
    var this_ = this;
    _.each(
        [FG.PointMarker, FG.RectFeature2, FG.MultiPolygonFeature, FG.MultiLineFeature],
        function(fclass) {
            this_.app.mapPanel.registerFeatureClass(fclass);
        }
    );
    
    _.each(
        [FG.PointWidget, FG.RectWidget],
        function(wclass) {
            this_.app.mapPanel.registerWidgetClass(wclass);
        }
    )
};

FG.InitApp.prototype.promptAddProject = function() {
    var this_ = this,
        app = this.app;
    
    if (app.toolModer.currentMode !== 'project') {
        return;
    }
    if (!DX.get('project:loaded', null)) {
        DX.alert('请先在左侧工程列表工具框中选择或创建一个工程, 再按提示添加所需的区域和坐标点.');
    }
};

FG.InitApp.prototype.connectSignals = function() {
    var this_ = this,
        app = this.app;
    
    DX.on('user_index_map:add!', function(m) {
        app.searchBox.pushIndexEntry(m) 
    });
    
    app.mapPanel.map.once('click dbclick', this.promptAddProject, this);
};

DX.on('app:pre_load_setting', function() {
// 初始的一些设置
//     alertify.set({ labels: {
//         ok     : "确定",
//         cancel : "取消"
//     } });
    alertify
        .okBtn('确定')
        .cancelBtn('取消')
        .logPosition('bottom right')
        .maxLogItems(16);

// TODO: 转为从后端加载配置
    DX.set({
        mapCoordinateType: 'degree'   // 'degree', 'dms'
    });
});

